package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyAuthAccess;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.entity.Distribute;
import cn.js.sangu.unitStore.entity.User;
import cn.js.sangu.unitStore.service.DistributeService;
import cn.js.sangu.unitStore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 说明：工单分发接口
 * 作者：linshuo
 * 日期：2024/8/7 8:40
 */
//@CrossOrigin //已有全局跨域设置CorsConfig
@RequestMapping("distribute")
@RestController
public class DistributeController {

    static final String OPERATION_DISTRIBUTE = "工单分发";

    @Autowired
    DistributeService distributeService;

    @Autowired
    UserService userService;

    @MyLogsAOP(operation = OPERATION_DISTRIBUTE,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum,
                        @RequestParam Integer pageSize,
                        @RequestParam String workName,
                        @RequestParam String creator,
                        @RequestParam String recipient){
        QueryWrapper<Distribute> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(workName),"work_name",workName);
        wrapper.eq(StrUtil.isNotBlank(creator),"creator",creator);
        wrapper.like(StrUtil.isNotBlank(recipient),"recipient",recipient);
        wrapper.orderByDesc("id");
        //构建page对象
        Page<Distribute> param = new Page<>(pageNum, pageSize);
        Page<Distribute> res = distributeService.page(param, wrapper);
        return R.ok(res.setRecords(suppleName(res.getRecords())));
    }

    @MyLogsAOP(operation = OPERATION_DISTRIBUTE,operaType = LogType.ADD)
    @RequestMapping("save")
    public R save(@RequestBody Distribute dto){
        Distribute distribute = distributeService.saveInfo(dto);
        return R.ok("《"+distribute.getWorkName()+"》保存成功");
    }

    @MyLogsAOP(operation = OPERATION_DISTRIBUTE,operaType = LogType.UPDATE)
    @PutMapping("update")
    public R update(@RequestBody Distribute dto){
        distributeService.updateInfo(dto);
        return R.ok("修改成功");
    }

    @MyLogsAOP(operation = OPERATION_DISTRIBUTE,operaType = LogType.DELETE)
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        distributeService.removeById(id);
        return R.ok("删除成功");
    }

    @MyLogsAOP(operation = OPERATION_DISTRIBUTE,operaType = LogType.BATCH_DELETE)
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        distributeService.removeBatchByIds(ids);
        return R.ok("批量删除成功");
    }

//    @MyLogsAOP(operation = OPERATION_distributeUMENT,operaType = LogType.LOOKUP)
    @MyAuthAccess
    @GetMapping("findById/{id}")
    public R findById(@PathVariable Integer id){
        Distribute distribute = distributeService.getById(id);
        User user = userService.getById(distribute.getCreator());
        if (null != user){
            distribute.setCreatorName(user.getName());
        }
        return R.ok(distribute);
    }

//    @MyLogsAOP(operation = OPERATION_distributeUMENT,operaType = LogType.LOOKUP)
    @GetMapping("findByRecent")
    public R findByRecent(){
        LambdaQueryWrapper<Distribute> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(Distribute::getCreatTime).last("LIMIT 10");
        return R.ok(suppleName(distributeService.list(wrapper)));
    }

    /**
     * 补充创建人+修改人+接收人 名称
     * @param list
     * @return
     */
    private List<Distribute> suppleName(List<Distribute> list ){
        for (Distribute distribute : list) {
            //创建人
            User author = userService.getById(distribute.getCreator());
            if (null != author) distribute.setCreatorName(author.getName());
            //修改人
            User modifier = userService.getById(distribute.getModifier());
            if (null != modifier) distribute.setModifierName(modifier.getName());
            //接收人
            User recipient = userService.getById(distribute.getRecipient());
            if (null != recipient) distribute.setRecipientName(recipient.getName());
        }
        return list;
    }

}
